package com.ruoyi.web.controller.business.audit.factory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.customer.service.JbCustomerService;
import com.ruoyi.business.order.domain.JbOrder;
import com.ruoyi.business.order.domain.JbPaymentReceived;
import com.ruoyi.business.order.domain.JbRefund;
import com.ruoyi.business.order.service.JbOrderService;
import com.ruoyi.business.order.service.JbPaymentReceivedService;
import com.ruoyi.business.order.service.JbRefundService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.web.controller.business.order.factory.JbApprovalLogFactory;
import com.ruoyi.web.controller.business.order.factory.JbOrderFactory;
import com.ruoyi.web.controller.business.order.factory.JbPaymentReceivedFactory;
import com.ruoyi.web.controller.business.order.factory.JbRefundFactory;
import com.ruoyi.web.controller.business.order.vo.*;
import com.ruoyi.web.controller.flyflow.factory.FlyFlowFactory;
import com.ruoyi.web.controller.flyflow.util.FlyFlowUtil;
import com.ruoyi.web.controller.flyflow.vo.FlyFlowTaskListVO;
import com.ruoyi.web.controller.flyflow.vo.FlyFloyFormatStartNodeShowVO;
import com.ruoyi.web.controller.thirdparty.factory.ThirdpartyExtendFactory;
import com.ruoyi.web.controller.thirdparty.factory.ThirdpartyOrderFactory;
import com.ruoyi.web.core.JoController;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 业务 审核 工厂
 *
 * @author Jyl
 */
@Log4j2
@Component
public class JbAuditFactory extends JoController
{

    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Autowired
    private JbOrderService jbOrderService;
    @Autowired
    private JbRefundService jbRefundService;
    @Autowired
    private JbPaymentReceivedService jbPaymentReceivedService;
    @Autowired
    private JbOrderFactory jbOrderFactory;
    @Autowired
    private JbRefundFactory jbRefundFactory;
    @Autowired
    private JbPaymentReceivedFactory jbPaymentReceivedFactory;
    @Autowired
    private JbApprovalLogFactory jbApprovalLogFactory;
    @Autowired
    private FlyFlowFactory flyFlowFactory;
    @Autowired
    private ThirdpartyOrderFactory thirdpartyOrderFactory;
    @Autowired
    private JbCustomerService jbCustomerService;
    @Autowired
    private ThirdpartyExtendFactory thirdpartyExtendFactory;

    /**
     * 撤销审批
     *
     * @param id 业务主键
     * @return 执行结果
     */
    public boolean revoke(Long id)
    {
        jbOrderService.update(new LambdaUpdateWrapper<JbOrder>()
                .eq(JbOrder::getId, id)
                .set(JbOrder::getOrderStatus, JbOrderStatusEnum.task6.getKey())
        );

        JbOrder jbOrder = jbOrderService.getById(id);
        jbApprovalLogFactory.saveApprovalLog(
                "撤销",
                jbOrder.getOrderNumber(),
                jbOrder.getProcessNumber(),
                "撤销",
                "撤销",
                "撤销"
        );
        return true;
    }

    /**
     * 撤销审批
     *
     * @param id 业务主键
     * @return 执行结果
     */
    public boolean end(Long id)
    {
        jbOrderService.update(new LambdaUpdateWrapper<JbOrder>()
                .eq(JbOrder::getId, id)
                .set(JbOrder::getOrderStatus, JbOrderStatusEnum.task13.getKey())
        );

        JbOrder jbOrder = jbOrderService.getById(id);
        jbApprovalLogFactory.saveApprovalLog(
                "撤销",
                jbOrder.getOrderNumber(),
                jbOrder.getProcessNumber(),
                "撤销",
                "撤销",
                "撤销"
        );
        return true;
    }

    /**
     * 订单待办
     */
    public JSONObject orderTask(PageParam param)
    {
        FlyFlowTaskListVO flowTaskListVO = FlyFlowTaskListVO.builder()
                .pageSize(param.getPageSize())
                .pageNum(param.getPageNum())
                .processName(JbAuditEnum.ORDER)
                .processId(param.getKeyword())
                .assignId(getUserId().toString())
                .build();
        JSONObject orderStatistics = taskList(flowTaskListVO);
        JSONObject data = orderStatistics.getJSONObject("data");
        JSONArray records = data.getJSONArray("records");

        List<String> number = new ArrayList<>();
        for (Object record : records)
        {
            JSONObject obj = (JSONObject) record;
            String processInstanceBizCode = obj.getString("bizCode");
            number.add(processInstanceBizCode);
        }
        if (number.isEmpty())
        {
            return new JSONObject();
        }

        List<JbOrder> list = jbOrderService.list(new LambdaQueryWrapper<JbOrder>()
                .in(JbOrder::getOrderNumber, number));
        Map<String, JbOrder> map = new HashMap<>();
        for (JbOrder order : list)
        {
            map.put(order.getOrderNumber(), order);
        }

        return getResult(records, map, data.getString("total"));
    }

    /**
     * 回款待办
     */
    public JSONObject paymentTask(PageParam param)
    {
        FlyFlowTaskListVO flowTaskListVO = FlyFlowTaskListVO.builder()
                .pageSize(param.getPageSize())
                .pageNum(param.getPageNum())
                .processName(JbAuditEnum.PAYMENTRECEIVED)
                .processId(param.getKeyword())
                .assignId(getUserId().toString())
                .build();
        JSONObject orderStatistics = taskList(flowTaskListVO);
        JSONObject data = orderStatistics.getJSONObject("data");
        JSONArray records = data.getJSONArray("records");

        List<String> number = new ArrayList<>();
        for (Object record : records)
        {
            JSONObject obj = (JSONObject) record;
            String processInstanceBizCode = obj.getString("bizCode");
            number.add(processInstanceBizCode);
        }
        if (number.isEmpty())
        {
            return new JSONObject();
        }

        List<JbPaymentReceived> list = jbPaymentReceivedService.list(new LambdaQueryWrapper<JbPaymentReceived>()
                .in(JbPaymentReceived::getPaymentNumber, number));
        Map<String, JbPaymentReceived> map = new HashMap<>();
        for (JbPaymentReceived received : list)
        {
            map.put(received.getPaymentNumber(), received);
        }

        return getResult(records, map, data.getString("total"));
    }

    /**
     * @param records 待办数据
     * @param map     业务数据
     * @param total   待办条数
     * @return 待办回调拼接业务信息
     */
    public JSONObject getResult(JSONArray records, Map map, String total)
    {
        JSONArray jsonArray = new JSONArray();
        for (Object record : records)
        {
            JSONObject obj = (JSONObject) record;
            String bizCode = obj.getString("bizCode");
            JSONObject jsonObject = new JSONObject();
            if (map.containsKey(bizCode))
            {
                jsonObject = JSONObject.parseObject(JSONObject.toJSONString(map.get(bizCode)));
                jsonObject.put("createTime", jsonObject.getDate("createTime"));
                jsonObject.put("updateTime", jsonObject.getDate("updateTime"));
            }

            JSONObject process = new JSONObject();
            process.put("taskId", obj.getString("taskId"));
            process.put("taskName", obj.getString("taskName"));
            process.put("bizCode", obj.getString("bizCode"));
            process.put("flowId", obj.getString("flowId"));

            process.put("processName", obj.getString("processName"));
            process.put("processInstanceId", obj.getString("processInstanceId"));
            process.put("processInstanceBizCode", obj.getString("processInstanceBizCode"));

            jsonObject.put("process", process);//流程对象数据
            jsonObject.putAll(process);//外层全集数据
            jsonArray.add(jsonObject);
        }
        JSONObject result = new JSONObject();
        result.put("records", jsonArray);
        result.put("total", total);
        return result;
    }

    /**
     * 退款退货待办
     */
    public JSONObject refundTask(PageParam param)
    {
        FlyFlowTaskListVO flowTaskListVO = FlyFlowTaskListVO.builder()
                .pageSize(param.getPageSize())
                .pageNum(param.getPageNum())
                .processName(JbAuditEnum.REFUND)
                .processId(param.getKeyword())
                .assignId(getUserId().toString())
                .build();
        JSONObject orderStatistics = taskList(flowTaskListVO);
        JSONObject data = orderStatistics.getJSONObject("data");
        JSONArray records = data.getJSONArray("records");

        List<String> number = new ArrayList<>();
        for (Object record : records)
        {
            JSONObject obj = (JSONObject) record;
            String processInstanceBizCode = obj.getString("bizCode");
            number.add(processInstanceBizCode);
        }
        if (number.isEmpty())
        {
            return new JSONObject();
        }

        List<JbRefund> list = jbRefundService.list(new LambdaQueryWrapper<JbRefund>()
                .in(JbRefund::getRefundNumber, number));
        Map<String, JbRefund> map = new HashMap<>();
        for (JbRefund refund : list)
        {
            map.put(refund.getRefundNumber(), refund);
        }

        return getResult(records, map, data.getString("total"));
    }

    /**
     * 待办列表
     *
     * @param flowTaskListVO 分页查询对象
     * @return 待办列表
     */
    public JSONObject taskList(FlyFlowTaskListVO flowTaskListVO)
    {
        return flyFlowFactory.queryTodoTaskList(flowTaskListVO);
    }

    /**
     * 所有待办列表 统计
     *
     * @param param 分页查询对象
     * @return 所有待办统计
     */
    public JSONObject taskListStatistics(PageParam param)
    {
        //创建工作流查询对象
        FlyFlowTaskListVO flowTaskListVO = FlyFlowTaskListVO.builder()
                .pageSize(param.getPageSize())
                .pageNum(param.getPageNum())
                .assignId(getUserId().toString())
                .build();

        //查询 JbAuditEnum.ORDER 订单
        JSONObject orderStatistics = taskList(flowTaskListVO.processName(JbAuditEnum.ORDER));
        //查询 JbAuditEnum.PAYMENTRECEIVED 回款
        JSONObject paymentReceivedStatistics = taskList(flowTaskListVO.processName(JbAuditEnum.PAYMENTRECEIVED));
        //查询 JbAuditEnum.REFUND 退款
        JSONObject refundStatistics = taskList(flowTaskListVO.processName(JbAuditEnum.REFUND));

        //构建工作流待办统计对象
        JbTaskStatisticsVO jbTaskStatisticsVO = JbTaskStatisticsVO.builder()
                .orderStatistics(Long.valueOf(FlyFlowUtil.analysisTaskTotal(orderStatistics)))
                .payumentReceivedorderStatistics(Long.valueOf(FlyFlowUtil.analysisTaskTotal(paymentReceivedStatistics)))
                .refundStatistics(Long.valueOf(FlyFlowUtil.analysisTaskTotal(refundStatistics)))
                .build();
        return JSONObject.parseObject(JSONObject.toJSONString(jbTaskStatisticsVO));
    }

    /**
     * 获取流程表单信息
     *
     * @param jbAuditVO
     */
    public void fnParamMap(JbAuditVO jbAuditVO)
    {
        JSONObject orderStatistics = flyFlowFactory.queryTodoTaskList(FlyFlowTaskListVO.builder()
                .processId(jbAuditVO.getProcessInstanceId())
                .pageNum(1).pageSize(10).build());
        JSONObject data = orderStatistics.getJSONObject("data");
        JSONArray records = data.getJSONArray("records");
        jbAuditVO.getFlyflow().put("paramMap", records.getJSONObject(0).getJSONObject("paramMap"));
    }

    /**
     * 审批业务
     *
     * @param jbAuditVO
     * @return
     * @Deprecated 业务审批模式已废弃，变更为从流程侧审批
     */
    @Deprecated
    @Transactional
    public boolean audit(JbAuditVO jbAuditVO)
    {
        List<SysDictData> list = sysDictTypeService.selectDictDataByType("bus_form_type");
        Map<String, String> dict = list.stream().collect(Collectors.toMap(
                SysDictData::getDictLabel,
                SysDictData::getDictValue));

        //字段重新赋值
        jbAuditVO.setTaskId(jbAuditVO.getFlyflow().getString("taskId"));
        fnParamMap(jbAuditVO);
        flyFlowFactory.sendTak(jbAuditVO.getFlyflow());

        jbApprovalLogFactory.saveApprovalLog(
                dict.get(jbAuditVO.getProcessName()),
                jbAuditVO.getBusinessCode(),
                jbAuditVO.getProcessInstanceId(),
                jbAuditVO.getTaskName(),
                jbAuditVO.getTaskName(),
                jbAuditVO.getFlyflow().getBoolean("approveResult") ? "通过" : "驳回"
        );

        fnProcess(jbAuditVO);
//        jbApprovalLogFactory.fnStatus(dict.get(jbAuditVO.getProcessName()), jbAuditVO);
        return true;
    }

    /**
     * 逻辑 流程
     *
     * @param jbAuditVO
     */
    public void fnProcess(JbAuditVO jbAuditVO)
    {
        switch (jbAuditVO.getProcessName())
        {
            case JbAuditEnum.ORDER -> fnOrder(jbAuditVO);
            case JbAuditEnum.PAYMENTRECEIVED -> fnPaymentReceived(jbAuditVO);
            case JbAuditEnum.REFUND -> fnRefund(jbAuditVO);
        }
    }

    /**
     * 逻辑 退款
     *
     * @param jbAuditVO
     */
    private void fnRefund(JbAuditVO jbAuditVO)
    {
        JbRefund jbRefund = jbRefundFactory.getInfoByProcessNumber(jbAuditVO.getProcessInstanceId());
        if (jbAuditVO.getFlyflow().getBoolean("approveResult"))
        {
            switch (jbAuditVO.getTaskName())
            {
                case JbAuditEnum.REFUND_EXECUTIVE_DIRECTOR ->
                {
                    jbRefund.setApprovalStatus(JbAuditStatusEnum.task1.getKey());
                }
                case JbAuditEnum.REFUND_FINANCE ->
                {
                    jbRefund.setApprovalStatus(JbAuditStatusEnum.task0.getKey());
                    jbRefund.setCommissionAmount(jbAuditVO.getJbRefund().getCommissionAmount());
                }
            }
        } else
        {
            jbRefund.setApprovalStatus(JbAuditStatusEnum.task2.getKey());
        }
        jbRefundService.updateById(jbRefund);
    }

    /**
     * 逻辑 回款
     *
     * @param jbAuditVO
     */
    private void fnPaymentReceived(JbAuditVO jbAuditVO)
    {
        JbPaymentReceived jbPaymentReceived =
                jbPaymentReceivedFactory.getInfoByProcessNumber(jbAuditVO.getProcessInstanceId());
        //业务与流程只有若关联性，做查询失败处理
        if (jbPaymentReceived == null)
        {
            log.debug("[{}]:{})({}",
                    "fnPaymentReceived",
                    jbAuditVO.getProcessInstanceId(),
                    "Process associated payment collection business failed");
            return;
        }

        if (jbAuditVO.getFlyflow().getBoolean("approveResult"))
        {
            JbOrder jbOrder = jbOrderFactory.getInfoByNumber(jbPaymentReceived.getRelatedOrder());
            if (jbOrder == null)
            {
                log.debug("[{}]:{})({}",
                        "fnPaymentReceived",
                        jbPaymentReceived.getRelatedOrder(),
                        "Payment associated with order failed");
                return;
            }

            //更新订单回款信息
            jbOrderService.update(new LambdaUpdateWrapper<JbOrder>()
                    .eq(JbOrder::getOrderNumber, jbPaymentReceived.getRelatedOrder())
                    .set(JbOrder::getReceivedAmount,
                            jbOrder.getReceivedAmount().add(jbPaymentReceived.getPaymentAmount()))
            );

            //更新业务审批状态
            jbPaymentReceived.setStatus(JbAuditStatusEnum.task0.getKey());
        } else
        {
            //更新业务审批状态
            jbPaymentReceived.setStatus(JbAuditStatusEnum.task2.getKey());
        }
        //更新业务审批状态 实体
        jbPaymentReceivedService.updateById(jbPaymentReceived);
    }

    /**
     * 逻辑 订单
     *
     * @param jbAuditVO
     */
    public void fnOrder(JbAuditVO jbAuditVO)
    {
        JbOrder jbOrder = jbOrderFactory.getInfoByProcessNumber(jbAuditVO.getProcessInstanceId());
        //业务与流程只有若关联性，做查询失败处理
        if (jbOrder == null)
        {
            log.debug("[{}]:{})({}",
                    "fnOrder",
                    jbAuditVO.getProcessInstanceId(),
                    "Process associated order business failed");
            return;
        }
        switch (jbAuditVO.getTaskName())
        {
            case JbAuditEnum.ORDER_BUSINESS -> fnOrderBusiness(jbAuditVO, jbOrder);
            case JbAuditEnum.ORDER_EXECUTIVE_DIRECTOR -> fnOrderExecutiveDirector(jbAuditVO, jbOrder);
            case JbAuditEnum.ORDER_LEADER -> fnOrderLeader(jbAuditVO, jbOrder);
            case JbAuditEnum.ORDER_FINANCE -> fnOrderFinance(jbAuditVO, jbOrder);
        }

    }

    /**
     * 订单状态
     *
     * @param jbAuditVO 审批对象
     * @param jbOrder   订单对象
     */
    public void fnOrderStatus(JbAuditVO jbAuditVO, JbOrder jbOrder)
    {
        //构建查询参数
        FlyFloyFormatStartNodeShowVO flyFloyFormatStartNodeShowVO = new FlyFloyFormatStartNodeShowVO();
        BeanUtils.copyProperties(jbAuditVO, flyFloyFormatStartNodeShowVO);
        flyFloyFormatStartNodeShowVO.setParamMap(jbAuditVO.getFlyflow().getJSONObject("paramMap"));

        //匹配 主管 领导 财务
        switch (flyFlowFactory.nextNode(flyFloyFormatStartNodeShowVO))
        {
            case JbAuditEnum.ORDER_EXECUTIVE_DIRECTOR -> jbOrder.setOrderStatus(JbOrderStatusEnum.task3.getKey());
            case JbAuditEnum.ORDER_LEADER -> jbOrder.setOrderStatus(JbOrderStatusEnum.task3_1.getKey());
            case JbAuditEnum.ORDER_FINANCE -> jbOrder.setOrderStatus(JbOrderStatusEnum.task4.getKey());
        }
    }

    /**
     * 逻辑 订单业务审批
     *
     * @param jbAuditVO
     * @param jbOrder
     */
    public void fnOrderBusiness(JbAuditVO jbAuditVO, JbOrder jbOrder)
    {
        if (jbAuditVO.getFlyflow().getBoolean("approveResult"))
        {
            fnOrderStatus(jbAuditVO, jbOrder);
        } else
        {
            jbOrder.setOrderStatus(JbOrderStatusEnum.task5.getKey());
        }

        String deptName = getUser().getDept().getDeptName();
        jbOrder.setBelongSalesZone(deptName);
        jbOrder.setBelongDepartment(deptName);
        jbOrder.setManagerAuditTime(new Date());
        jbOrder.setManagerAuditResult(jbAuditVO.getFlyflow().getBoolean("approveResult") ? "通过" : "驳回");

        jbOrderService.updateById(jbOrder);
    }

    /**
     * 逻辑 订单主管审批
     *
     * @param jbAuditVO
     * @param jbOrder
     */
    public void fnOrderExecutiveDirector(JbAuditVO jbAuditVO, JbOrder jbOrder)
    {
        if (jbAuditVO.getFlyflow().getBoolean("approveResult"))
        {
            fnOrderStatus(jbAuditVO, jbOrder);
        } else
        {
            jbOrder.setOrderStatus(JbOrderStatusEnum.task5.getKey());
        }

        jbOrder.setSupervisorAuditTime(new Date());
        jbOrder.setSupervisorAuditResult(jbAuditVO.getFlyflow().getBoolean("approveResult") ? "通过" : "驳回");

        jbOrderService.updateById(jbOrder);
    }

    /**
     * 逻辑 订单领导审批
     *
     * @param jbAuditVO
     * @param jbOrder
     */
    public void fnOrderLeader(JbAuditVO jbAuditVO, JbOrder jbOrder)
    {
        if (jbAuditVO.getFlyflow().getBoolean("approveResult"))
        {
            fnOrderStatus(jbAuditVO, jbOrder);
        } else
        {
            jbOrder.setOrderStatus(JbOrderStatusEnum.task5.getKey());
        }

        jbOrderService.updateById(jbOrder);
    }

    /**
     * 逻辑 订单财务审批
     *
     * @param jbAuditVO
     * @param jbOrder
     */
    public void fnOrderFinance(JbAuditVO jbAuditVO, JbOrder jbOrder)
    {
        if (jbAuditVO.getFlyflow().getBoolean("approveResult"))
        {
            jbOrder.setOrderStatus(JbOrderStatusEnum.task8.getKey());
            //财务审批通过发布用友销售订单，
            jbOrderFactory.fnThirdpartyOrderCreate(jbOrder);
        } else
        {
            jbOrder.setOrderStatus(JbOrderStatusEnum.task5.getKey());
        }

        jbOrder.setFinanceAuditTime(new Date());
        jbOrder.setFinanceAuditResult(jbAuditVO.getFlyflow().getBoolean("approveResult") ? "通过" : "驳回");

        jbOrderService.updateById(jbOrder);
    }

    /**
     * 查询业务对象 根据 流程实例id
     *
     * @param processId 流程实例主键
     * @param result    执行结果 1通过 2否决 3撤销
     */
    public Boolean updateBussines(String processId, Integer result)
    {
        //1.查询业务对象
        JSONObject jsonObject = jbOrderService.findBusinessByProcessId(processId);
        if (jsonObject.isEmpty())
        {
            log.error("[{}]:{}>>>{}", "流程完结通知(Process completion notification)", "失败(fail)1", processId);
            return false;
        }

        String businessCode = jsonObject.getString("businessCode");
        String businessType = businessCode.substring(0, 2);
        Long id = Long.valueOf(jsonObject.getString("id"));

        log.info("[{}]:{}>>>{}", "流程完结通知(Process completion notification)", "查询信息(info)", jsonObject);

        //2.更新业务对象
        Boolean b = switch (businessType)
        {
            case "DD" -> jbOrderFactory.updateBusinessStatus(id, switchOrderStatus(result));
            case "RE" -> jbRefundFactory.updataBusinessStatus(id, switchBusinessStatus(result));
            case "PR" -> fnPRSendOrder(id, result);
            default -> false;
        };
        if (!b)
        {
            log.error("[{}]:{}>>>{}", "流程完结通知(Process completion notification)", "失败(fail)3", processId);
            return false;
        }

        return true;
    }

    /**
     * 逻辑 回款结束 通知 订单更新
     *
     * @param id     回款单主键
     * @param result 审批结果
     * @return 执行结果
     */
    public Boolean fnPRSendOrder(Long id, Integer result)
    {
        /*
         * 因工作流是先关闭流程，再变更状态:
         *  1.工作流先关闭流程，触发一次完结通知，且pass状态无法判别是否真实完结操作，
         *  2.工作流变更撤销状态，再触发一次完结通知，
         * 后续 工作流完结通知，工作流状态变更撤销通知 去除已撤销或被否决的
         */
        JbPaymentReceived prStatus = jbPaymentReceivedFactory.getService().getById(id);
        if (prStatus.getStatus().equals(JbAuditStatusEnum.task2.getKey())
            || prStatus.getStatus().equals(JbAuditStatusEnum.task3.getKey())
        )
        {
            return true;
        }

        // 更新回款结果
        boolean b = jbPaymentReceivedFactory.updataBusinessStatus(id, switchBusinessStatus(result));

        // 工作流审批不通过，跳出，不通知订单
        if (result != 1)
        {
            log.info("[{}]:{}", "审批流程不通过，不通知订单更新回款金额", id);
            return false;
        }

        // 通知订单
        JbPaymentReceived jbPaymentReceived = jbPaymentReceivedFactory.getService().getById(id);
        JbOrder jbOrder = jbOrderFactory.getService().getOne(new LambdaQueryWrapper<JbOrder>()
                .eq(JbOrder::getOrderNumber, jbPaymentReceived.getRelatedOrder()));

        BigDecimal re = jbOrder.getReceivedAmount();
        BigDecimal order_amount = jbOrder.getOrderTotalAmount();
        re = re.add(jbPaymentReceived.getPaymentAmount());
        jbOrder.setReceivedAmount(re);
        if (order_amount.compareTo(re) < 1)
        {
            jbOrder.setOrderStatus(JbOrderStatusEnum.task11.getKey());
            jbOrder.setIsBusinessEnd(true);
        }

        //更新订单信息
        return jbOrderFactory.updateReceivedAmount(jbOrder);
    }

    /**
     * 订单状态处理
     *
     * @param result 工作流审批结果
     * @return 订单状态
     */
    public String switchOrderStatus(Integer result)
    {
        return switch (result)
        {
            case 1 -> JbOrderStatusEnum.task1.getKey();
            case 2 -> JbOrderStatusEnum.task5.getKey();
            case 3 -> JbOrderStatusEnum.task6.getKey();
            default -> "";
        };
    }

    /**
     * 其他业务状态处理
     *
     * @param result 工作流审批结果
     * @return 其他业务状态
     */
    public String switchBusinessStatus(Integer result)
    {
        return switch (result)
        {
            case 1 -> JbAuditStatusEnum.task0.getKey();
            case 2 -> JbAuditStatusEnum.task2.getKey();
            case 3 -> JbAuditStatusEnum.task3.getKey();
            default -> "";
        };
    }

    /**
     * 启动流程
     */
    public void startProcess()
    {

    }

    /**
     * 通过流程
     */
    public void passProcess()
    {

    }

    /**
     * 驳回流程
     */
    public void rejectProcess()
    {

    }

    /**
     * 否决
     */
    public void vetoProcess()
    {

    }

    /**
     * 转审
     */
    public void assignProcess()
    {

    }

    /**
     * 撤销
     */
    public void rescindProcess()
    {

    }

}
